<?php

namespace app\api\controller;

use app\api\service\shop\MyInfoService;
use app\api\validate\OrderValidate;

class MyInfo extends Base
{


    protected $noNeedRight = ['getMyProfit', 'getMyInfo', 'getMyProfitList', 'getMyShelvesList', 'getMyShelves'];
    private MyInfoService $myInfoService;

    public function __construct(MyInfoService $myInfoService, OrderValidate $orderValidate)
    {
        parent::__construct();
        $this->myInfoService = $myInfoService;
        $this->validate = $orderValidate;
    }


    /**
     * 获取我的信息
     */
    public function getMyInfo()
    {
        $userId = intval($this->auth->id);
        $companyId = intval($this->auth->company_id);
        $companyName = $this->auth->company_name;
        $nickname = $this->auth->nickname;
        $avatar = $this->auth->avatar;
        $data = ['userId' => $userId, 'company_id' => $companyId, 'company_name' => $companyName, 'nickname' => $nickname, 'avatar' => $avatar];
        $data['myProfit'] = $this->myInfoService->getProfitTotal($userId); // 总收益
        // 获取今日的起始时间戳（即当天的00:00:00）
        $startOfToday = strtotime('today midnight');
        // 获取今日的结束时间戳（即当天的23:59:59）
        $endOfToday = strtotime('tomorrow midnight') - 1;
        $data['dayProfit'] = $this->myInfoService->getProfitBetween($userId, $startOfToday, $endOfToday); // 今日收益
        $data['shelves'] = $this->myInfoService->getMyShelves($userId)['total']; // 我上架的
        $data['buy'] = $this->myInfoService->getBuy($userId, $companyId);  // 我购买的
        $data['sell'] = $this->myInfoService->getSell($userId, $companyId); // 我出售的
        $this->success('ok', $data);
    }

    /**
     * 获取我上架的数量
     */
    public function getMyShelves()
    {
        $userId = intval($this->auth->id);
        $this->success('ok', $this->myInfoService->getMyShelves($userId));
    }

    /**
     * 获取我的收益
     */
    public function getMyProfit()
    {
        $userId = intval($this->auth->id);
        $data['myProfit'] = $this->myInfoService->getProfitTotal($userId);
        // 获取今日的起始时间戳（即当天的00:00:00）
        $startOfToday = strtotime('today midnight');
        // 获取今日的结束时间戳（即当天的23:59:59）
        $endOfToday = strtotime('tomorrow midnight') - 1;
        $data['dayProfit'] = $this->myInfoService->getProfitBetween($userId, $startOfToday, $endOfToday);
        $currentTime = time();
        // 获取当前月份的起始时间戳
        $startOfMonth = strtotime(date('Y-m-01 00:00:00', $currentTime));
        // 获取当前月份的结束时间戳
        $endOfMonth = strtotime(date('Y-m-t 23:59:59', $currentTime));
        $data['monthProfit'] = $this->myInfoService->getProfitBetween($userId, $startOfMonth, $endOfMonth);
        $this->success('ok', $data);
    }

    /**
     * 获取我的收益列表
     */
    public function getMyProfitList()
    {
        $data = $this->request->get();
        $this->valid($data, 'profit');
        $userId = intval($this->auth->id);
        $typeId = $this->request->param('typeId');
        $this->success('ok', $this->myInfoService->getProfitRecord($userId, $data['page'], $data['limit'], $typeId));
    }

    /**
     * 获取我上架的列表
     */
    public function getMyShelvesList()
    {
        $data = $this->request->get();
        $this->valid($data, 'shelves');
        $userId = intval($this->auth->id);
        $this->success('ok', $this->myInfoService->getMyShelvesList($userId, $data['type'], $data['page'], $data['limit']));
    }

}